package models

import (
	"context"
	"fmt"
	"time"

	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

var username string = "xusun"
var password string = "xusun000"
var url string = "mongodb://124.223.62.14:27017"

var Mongo = initMongo()

func initMongo() *mongo.Database {

	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()
	client, err := mongo.Connect(ctx, options.Client().SetAuth((options.Credential{
		Username: username,
		Password: password,
	})).ApplyURI(url))
	if err != nil {
		fmt.Println("出错了")
	}
	return client.Database("sensor")
}
